package top.goldenyear.porpoise.infra.storage.controller.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.UploadedFile;
import top.goldenyear.porpoise.common.core.model.Ret;
import top.goldenyear.porpoise.framework.web.core.controller.BaseController;
import top.goldenyear.porpoise.framework.web.core.model.query.QPage;
import top.goldenyear.porpoise.infra.storage.model.StorageFile;
import top.goldenyear.porpoise.infra.storage.service.StorageFileService;

/**
 * 文件存储表 Controller 控制层
 *
 * @author airhead
 * @since 2024-10-16
 */
@Component(tag = "adminApi")
@Mapping("/infra/storage/storageFile")
@Slf4j
@Api("文件管理")
public class StorageFileAdminController extends BaseController {
  @Inject private StorageFileService service;

  @Post
  @Mapping("create")
  @ApiOperation("创建文件")
  public Ret create(@Body StorageFile files) {
    return Ret.okWithData(service.createFile(files));
  }

  @Post
  @Put
  @Mapping("update")
  public Ret update(@Body StorageFile files) {
    return Ret.okWithData(service.updateFile(files));
  }

  @Get
  @Mapping("get")
  public Ret get(@Param Integer id) {
    return Ret.okWithData(service.getFile(id));
  }

  @Post
  @Delete
  @Mapping("delete")
  public Ret delete(@Param Integer id) {
    return Ret.okWithData(service.deleteFile(id));
  }

  @Get
  @Mapping("paginate")
  public Ret paginate(QPage qPage) {
    StorageFile files = getBean(StorageFile.class);
    return Ret.okWithData(service.paginateFile(qPage, files));
  }

  @Get
  @Mapping("list")
  @ApiOperation("文件列表")
  public Ret list() {
    StorageFile files = getBean(StorageFile.class);
    return Ret.okWithData(service.listFile(files));
  }

  @ApiOperation("上传文件")
  @Post
  @Mapping("uploadFile")
  public Ret uploadFile(Integer configId, UploadedFile file) throws IOException {
    return Ret.okWithData(service.uploadFile(configId, file));
  }

  @ApiOperation("上传文件")
  @Post
  @Mapping("uploadFiles")
  public Ret uploadFiles(Integer configId, List<UploadedFile> files) throws IOException {
    return Ret.okWithData(service.uploadFiles(configId, files));
  }

  @Get
  @Mapping("/{configId}/{fileName}")
  @SaIgnore
  @ApiOperation("查看文件")
  public void download(Context context, @Path Integer configId, @Path String fileName)
      throws IOException {
    service.download(context, configId, fileName);
  }
}
